<?php
App::import('Sanitize');
class RatingsController extends AppController {
    var $name = 'Ratings';
    function rate($user_id, $product_id, $value) {
        $this->autoRender = false;

        $conditions = array('conditions' => array('Rating.user_id' => $user_id, 'Rating.product_id' => $product_id));

        if ($this->Rating->find('count', $conditions)) {
            $rate = $this->Rating->find('first', $conditions);
            $rate['Rating']['value'] = $value;
            $this->Rating->create();
            $this->Rating->save($rate);
        } else {
            $this->Rating->create();
            $data = compact(array('user_id', 'product_id', 'value'));
            $this->Rating->save($data);
        }
        print_r('Your rating is successfully recorded');
    }
}
